/* $Id tongying.cai@gmail.com 2012/5/10 13:32:38 Exp$ */
#include <bolt.h>
#include <prot.h>

BOLT(tcp_show, "tcp show")
{
   tcp_show();
}

BOLT(tcp_dae, "tcp <id> daemon <src-ip> <src-port>")
{
   int id;
   uint32_t tcp_id;
   sockaddr_t src;
   uint16_t port;
   memset(&src, 0, sizeof(src));
   if (!str2u16(pop_argz(), &port)) {
      printf("Invalid source port.\n");
      return;
   }
   if (!ip2addr(pop_argz(), &src.v0)) {
      printf ("Invalid source IP.\n");
      return;
   }
   addr_set_port(&src, port);
   if (!str2u32(pop_argz(), &tcp_id)) {
      printf("Invalid tcp id.\n");
      return;
   }
   id = tcp_daemon((int)tcp_id, &src);
   printf("The tcp id is %d\n", id);
}

BOLT(tcp_client, "tcp <id> client <src-ip> <src-port> <dst-ip> <dst-port>")
{
   int id;
   uint32_t tcp_id;
   sockaddr_t src, dst;
   uint16_t port;
   memset(&src, 0, sizeof(src));
   memset(&dst, 0, sizeof(dst));
   if (!str2u16(pop_argz(), &port)) {
      printf("Invalid destination port.\n");
      return;
   }
   if (!ip2addr(pop_argz(), &dst.v0)) {
      printf ("Invalid destination IP.\n");
      return;
   }
   addr_set_port(&dst, port);
   if (!str2u16(pop_argz(), &port)) {
      printf("Invalid source port.\n");
      return;
   }
   if (!ip2addr(pop_argz(), &src.v0)) {
      printf ("Invalid source IP.\n");
      return;
   }
   addr_set_port(&src, port);
   if (!str2u32(pop_argz(), &tcp_id)) {
      printf("Invalid tcp id.\n");
      return;
   }
   id = tcp_client((int)tcp_id, &src, &dst);
   printf("The tcp id is %d\n", id);
}


BOLT(tcp_release, "tcp <id> release")
{
   uint32_t id;
   if (str2u32(pop_argz(), &id)) {
      tcp_release((int)id);   
   } else {
      printf("Invalid tcp id.\n");
   }
}

#define TCP_TEST_LEN 20
static uint8_t tcp_test_buf[TCP_TEST_LEN];

BOLT(tcp_send, "tcp <id> send <number>")
{
   uint32_t i, num, id;

   if (!str2u32(pop_argz(), &num)) {
      printf("Invalid number\n");
      return;
   }
      
   if (!str2u32(pop_argz(), &id)) {
      printf("Invalid ID\n");
      return;
   }
   for (i = 0; i < num; ++i) {
      int len = tcp_send((int)id, tcp_test_buf, TCP_TEST_LEN); 
      printf("%u send tcp in id %u, len=%d\n", i, id, len);
   }
}
     
BOLT(tcp_echo_disable, "tcp echo disable")
{
   tcp_echo_disable();
}
   
BOLT(tcp_echo_enable, "tcp echo enable")
{
   tcp_echo_enable();
}
